{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5331ba2-c925-47e9-a53d-0d41ff18fad0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import graphviz\n",
    "import lingam\n",
    "from lingam.utils import make_dot\n",
    "import logging\n",
    "import dowhy\n",
    "from dowhy import CausalModel\n",
    "from semopy import Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7703fc69-2dce-4f73-972c-81033616d08e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"\\df_1_2.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85638834-05bc-4f26-b549-3eec490a1b12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the SEM model\n",
    "model_desc = \"\"\"\n",
    "Distrust ~ Loneliness + income + education + age + sex + marriage + religion + ideology\n",
    "Indifference ~ Loneliness + income + education + age + sex + marriage + religion + ideology\n",
    "\n",
    "Loneliness ~ Multicultural + income + education + age + sex + marriage + religion + ideology\n",
    "Loneliness ~ Homeownership + income + education + age + sex + marriage + religion + ideology\n",
    "\n",
    "Distrust ~ Multicultural + income + education + age + sex + marriage + religion + ideology\n",
    "Indifference ~ Multicultural + income + education + age + sex + marriage + religion + ideology\n",
    "\n",
    "Distrust ~ Homeownership + income + education + age + sex + marriage + religion + ideology\n",
    "Indifference ~ Homeownership + income + education + age + sex + marriage + religion + ideology\n",
    "\"\"\"\n",
    "\n",
    "# Load your data (make sure 'df' is already defined or load it accordingly)\n",
    "# Example:\n",
    "# df = pd.read_csv(\"your_data.csv\")\n",
    "\n",
    "# Create and fit the model\n",
    "model = Model(model_desc)\n",
    "results = model.fit(df)\n",
    "params = model.inspect()\n",
    "\n",
    "# Print full SEM results\n",
    "print(\"Full SEM parameter estimates:\\n\")\n",
    "print(params)\n",
    "\n",
    "# Optionally display in tabular format (useful for Jupyter)\n",
    "pd.set_option('display.max_rows', None)\n",
    "pd.set_option('display.max_columns', None)\n",
    "display(params)\n",
    "\n",
    "# Export to CSV if desired\n",
    "params.to_csv(\"sem_full_results.csv\", index=False)\n",
    "\n",
    "# Grouped output for interpretability\n",
    "print(\"\\n--- Grouped Results by Outcome Variable ---\")\n",
    "outcomes = ['Distrust', 'Indifference', 'Loneliness']\n",
    "for target in outcomes:\n",
    "    subset = params[(params['op'] == '~') & (params['lval'] == target)]\n",
    "    if not subset.empty:\n",
    "        print(f\"\\nResults for {target}:\\n\", subset[['lval', 'op', 'rval', 'Estimate', 'Std.Err', 'z-value', 'p-value']])\n",
    "\n",
    "# Function to visualize the SEM model with selected variables\n",
    "def visualize_sem_with_focused_params(model, params, focus_vars, path=\"sem_graph\", width=\"80\", height=\"120\", fontsize=\"14\"):\n",
    "    dot = graphviz.Digraph(comment='Focused SEM Model')\n",
    "    dot.attr(size=f\"{width},{height}\", ratio='auto')\n",
    "\n",
    "    focus_set = set(focus_vars)\n",
    "\n",
    "    # Add nodes\n",
    "    for var in focus_set:\n",
    "        dot.node(var, var, shape='ellipse')\n",
    "\n",
    "    # Add edges with estimates and significance\n",
    "    for _, row in params.iterrows():\n",
    "        if row['op'] == '~' and {row['lval'], row['rval']}.issubset(focus_set):\n",
    "            estimate = round(row['Estimate'], 3)\n",
    "            p_value = round(row['p-value'], 3)\n",
    "            significance = '***' if p_value < 0.01 else '**' if p_value < 0.05 else '* ' if p_value < 0.1 else ''\n",
    "            label = f'est: {estimate}, p: {p_value} {significance}'\n",
    "            dot.edge(row['rval'], row['lval'], label=label, fontsize=fontsize)\n",
    "\n",
    "    # Render and save\n",
    "    dot.render(path, view=True)\n",
    "\n",
    "# Specify variables to focus on in visualization\n",
    "focus_vars = [\"Indifference\", \"Distrust\", \"Loneliness\", \"Homeownership\", \"Multicultural\"]\n",
    "\n",
    "# Visualize the SEM model\n",
    "visualize_sem_with_focused_params(model, params, focus_vars, \"focused_sem_output\", width=\"100\", height=\"80\", fontsize=\"14\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e629e458-13cf-46a6-88b4-6245b4c9bda9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2185cdca-d9f3-4c5b-bc92-338a76a9597b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
